/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ErrorDialog.java
 *
 * 
 * Created on May 10, 2010, 4:33:57 PM
 */

package net.worlds.component;

import java.awt.Desktop;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URL;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import net.worlds.Main;
import net.worlds.Util;

/**
 *
 * @author Jacob Morgan
 */
public class ErrorDialog extends javax.swing.JFrame {
    public ErrorDialog() {
        initComponents();
        try
        {
            setIconImages(Util.icons);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public ErrorDialog(String exception, boolean redownload)
    {
        initComponents();
        exceptionLabel.setText(exception);
        if(redownload)
        {
            titleLabel.setText("<html>Startup Error - <a href=\"http://"+Main.siteURL+"/editor.php?update\">Redownloading the game may fix this problem.</a>");
            titleLabel.addMouseListener(new MouseListener()
        {
                public void mouseClicked(MouseEvent e) {
                    if(Desktop.isDesktopSupported())
                    {
                        try {
                            Desktop.getDesktop().browse(new URL("http://"+Main.siteURL+"/editor.php?update").toURI());
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }
                    }
                }

                public void mousePressed(MouseEvent e) {

                }

                public void mouseReleased(MouseEvent e) {

                }

                public void mouseEntered(MouseEvent e) {

                }

                public void mouseExited(MouseEvent e) {

                }
        });
        }
        pack();
        if(Main.getView() != null && Main.getApplet() != null)
            setLocationRelativeTo(Main.getApplet());
    }

    private void initComponents() {

        titleLabel = new javax.swing.JLabel();
        errorLabel = new javax.swing.JLabel();
        exceptionLabel = new javax.swing.JLabel();
        exitButton = new javax.swing.JButton();
        continueButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        titleLabel.setText("2DWorlds Error");

        exitButton.setText("Exit");
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });

        continueButton.setText("Continue");
        continueButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                continueButtonActionPerformed(evt);
            }
        });

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(Alignment.LEADING)
                    .addComponent(titleLabel)
                    .addComponent(errorLabel)
                    .addComponent(exceptionLabel))
                .addContainerGap(249, Short.MAX_VALUE))
            .addGroup(Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(245, Short.MAX_VALUE)
                .addComponent(continueButton)
                .addGap(18, 18, 18)
                .addComponent(exitButton)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(titleLabel)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(errorLabel)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(exceptionLabel)
                .addPreferredGap(ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(exitButton)
                    .addComponent(continueButton))
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {
        System.exit(1);
    }

    private void continueButtonActionPerformed(java.awt.event.ActionEvent evt) {
        dispose();
    }

    private javax.swing.JButton continueButton;
    private javax.swing.JLabel errorLabel;
    private javax.swing.JLabel exceptionLabel;
    private javax.swing.JButton exitButton;
    private javax.swing.JLabel titleLabel;
}
